<?php
defined('APP_PATH') or die ('Unauthorized access');
/**
 * 分页扩展类
 */
class Ext_Page
{
	public $config;
	
	public function __construct()
	{
		
	}
	
	/**
	 * 分页
	 * @return string
	 */
	public function layOut()
	{
		if(empty($this->config)) Error::message('分页配置未定义！');
		
		
		$total_page = ceil($this->config['total'] / $this->config['pagesize']);
		
		$page = '';
		
		$page .= $this->config['page'] == 1 ? '<li>首页</li>' : "<li><a href='{$this->config['url']}1'>首页</a></li>";
		
		$page .= $this->config['page'] > 1 ? "<li><a href='{$this->config['url']}".($this->config['page']-1)."'>前一页</a></li>" : '';
		
		if($total_page > $this->config['size'] && $this->config['page'] >= $this->config['size']){
			if($this->config['page'] <= $total_page){
				for($i = $this->config['page'] - 4; $i < $this->config['page'] - 4 + $this->config['size'];$i ++){
					if($i > $total_page) break;
					$thisclass = $this->config['page'] == $i ? " class='thisclass'" : '';
					if($thisclass) $page .= "<li {$thisclass}>{$i}</li>";
					else $page .= "<li {$thisclass}><a href='{$this->config['url']}{$i}'>{$i}</a></li>";
				}
			}else{
				Error::message("当前页超出总页数({$total_page})");
			}
		}else{
			if($this->config['size'] < $total_page){
				for($i = 1;$i <= $this->config['size'];$i++){
					$thisclass = $this->config['page'] == $i ? 'class="thisclass"' : '';
					if($thisclass) $page .= "<li {$thisclass}>{$i}</li>";
					else $page .= "<li {$thisclass}><a href='{$this->config['url']}{$i}'>{$i}</a></li>";			
				}
			}else{
				for($i = 1;$i <= $total_page;$i++){
					$thisclass = $this->config['page'] == $i ? 'class="thisclass"' : '';
					if($thisclass) $page .= "<li {$thisclass}>{$i}</li>";
					else $page .= "<li {$thisclass}><a href='{$this->config['url']}{$i}'>{$i}</a></li>";	
				}
			}
		}
		
		$page .= $this->config['page'] != $total_page ? "<li><a href='{$this->config['url']}".($this->config['page']+1)."'>下一页</a></li>" : '';
		
		$page .= $this->config['page'] == $total_page ? '<li>末页</li>' : "<li><a href='{$this->config['url']}{$total_page}'>末页</a></li>";
		
		$page .= "<li><span> 共<strong>{$total_page}</strong>页 <strong>{$this->config['total']}</strong>条</span></li>";
		return $page;
	}
}